
DEFINE TEMP-TABLE rod
    FIELDS rodmth AS   CHARACTER 
    FIELDS rodrouting  LIKE ro_routing
    FIELDS rodop       LIKE ro_op
    FIELDS rodrun      LIKE ro_run
    FIELDS roddec01    LIKE ro__dec01
    INDEX rodmth rodmth rodrouting rodop
    .

DEFINE TEMP-TABLE rostd
    FIELDS rosrouting LIKE ro_routing
    FIELDS rosrun     LIKE ro_run
    INDEX rosrouting rosrouting
    .
DEFINE TEMP-TABLE wot
    FIELDS wotmth  AS   CHARACTER
    FIELDS wotpart LIKE wo_part
    FIELDS wotnbr  LIKE wo_nbr
    FIELDS wotdept LIKE wo__chr01
    FIELDS wotqty  LIKE wo_qty_comp
    INDEX  wthmpd wotmth wotdept wotpart
    .

DEFINE VARIABLE rorun   LIKE ro_run NO-UNDO.
DEFINE VARIABLE qtycomp LIKE wo_qty_comp NO-UNDO.

INPUT FROM \\192.168.12.12\shared$\output\30\R30051.csv.
REPEAT:
    CREATE rod.
    IMPORT DELIMITER "," rodmth rodrouting rodop rodrun roddec01 NO-ERROR.
END.
INPUT CLOSE.

FOR EACH rod NO-LOCK WHERE  rodmth = '2009M03' AND rodrouting <> '' BREAK BY rodrouting:
    IF FIRST-OF (rodrouting) THEN DO:
        ASSIGN rorun = 0.
    END.
    IF roddec01 <> 0 THEN DO:
        rorun = rorun + roddec01.
    END.
    ELSE DO:
        rorun = rorun + rodrun.
    END.
    IF LAST-OF (rodrouting) THEN DO:
        CREATE rostd.
        ASSIGN rosrouting = rodrouting
               rosrun = rorun.
    END.                      
END.

OUTPUT TO rorun.txt.
FOR EACH rostd NO-LOCK WHERE  :
  DISPLAY rostd WITH WIDTH 200 STREAM-IO.
END.
OUTPUT CLOSE.
 
FOR EACH wo_mstr NO-LOCK WHERE wo__chr01 <> '' AND wo_stat_close_date >= DATE(5,1,2009) AND wo_stat_close_date <=DATE(7,31,2009)  :
    CREATE wot.
    ASSIGN wotmth =  string(YEAR(wo_stat_close_date),"9999") + "M" + STRING(MONTH(wo_stat_close_date),"99")
           wotpart = wo_part
           wotnbr  = wo_nbr
           wotqty  = wo_qty_comp
           wotdept = IF SUBSTRING(wo__chr01,1,4)='72F5' THEN '72F500' ELSE SUBSTRING(wo__chr01,1,3) + '000'.
END.


OUTPUT TO w.csv.
PUT "mth,dept,part,comp,run" SKIP.
FOR EACH wot NO-LOCK WHERE  BREAK BY wotmth BY wotdept BY wotpart:
    IF FIRST-OF (wotpart) THEN DO:
       ASSIGN qtycomp = 0.
   END.
   qtycomp = qtycomp + wotqty.
   IF LAST-OF(wotpart) THEN DO:
       FIND FIRST rostd NO-LOCK WHERE rosrouting = wotpart NO-ERROR.
       IF AVAIL rostd THEN DO:
           EXPORT DELIMITER "," wotmth wotdept wotpart qtycomp rosrun.
       END.
   END.
 END.

OUTPUT CLOSE.
